package cn.alusiaor.common.util;

import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author alusiaor
 * @title: CloneUtil
 * @projectName learnspringboot
 * @description: TODO
 * @date 2019/12/2115:44
 */

public class CloneUtil {

    private static Mapper dozerMapper = new DozerBeanMapper();

    /**
     * @param s   数据对象
     * @param clz 复制目标类型
     * @Description: 单个对象的深度复制及类型转换，vo/domain , po
     */
    public static <T, S> T convert(S s, Class<T> clz) {
        if (s == null) {
            return null;
        }
        return dozerMapper.map(s, clz);
    }


    /**
     * @param s   数据对象
     * @param clz 复制目标类型
     * @Description: list深度复制
     */


    public static <T, S> List<T> convert(List<S> s, Class<T> clz) {
        if (s == null) {
            return null;
        }
        List<T> list = new ArrayList<T>();
        for (S vs : s) {
            list.add(dozerMapper.map(vs, clz));
        }
        return list;
    }

    /**
     * @param s   数据对象
     * @param clz 复制目标类型
     * @Description: set深度复制
     */
    public static <T, S> Set<T> convert(Set<S> s, Class<T> clz) {
        if (s == null) {
            return null;
        }
        Set<T> set = new HashSet<T>();
        for (S vs : s) {
            set.add(dozerMapper.map(vs, clz));
        }
        return set;
    }

    /**
     * @param s   数据对象
     * @param clz 复制目标类型
     * @Description: 数组深度复制
     */
    public static <T, S> T[] convert(S[] s, Class<T> clz) {
        if (s == null) {
            return null;
        }
        @SuppressWarnings("unchecked")
        T[] arr = (T[]) Array.newInstance(clz, s.length);
        for (int i = 0; i < s.length; i++) {
            arr[i] = dozerMapper.map(s[i], clz);
        }
        return arr;
    }

/**
 * 方法获取自己本身的注解
 */
//    public static PlatformInterfaceDTO getCallInterface(Class<?> clazz, Class<?>... parameterTypes) {
//        try {
//            Method method = clazz.getMethod(Thread.currentThread().getStackTrace()[2].getMethodName(), parameterTypes);
//            VisitedApi visitedApi = method.getAnnotation(VisitedApi.class);
//            PlatformInterfaceDTO callInterface = getBean(PlatformInterfaceService.class).queryCallInterfaceByInterfaceIdentityAndPlatformId(visitedApi.targetAPIIdentity(), visitedApi.sourcePlatFormId());
//            return callInterface;
//        } catch (Exception e) {
//            throw new IgcException(IgcErrorCode.GET_CALL_INTERFACE_ERROR);
//        }
//    }

}
